﻿using Microsoft.Practices.Prism.Mvvm;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm.Interfaces;
using Microsoft.Practices.Prism.PubSubEvents;

namespace Demo01.ViewModels
{
    public interface IMainPageViewModel
    {
        ObservableCollection<Models.Item> Items { get; }
        Models.Item Selected { get; set; }
        DelegateCommand AboutCommand { get; }
        DelegateCommand OpenCommand { get; }
        int Value { get; set; }
    }

    public class MainPageViewModel : ViewModel
    {
        INavigationService _navigationService;
        Repositories.IItemRepository _itemRepository;
        IEventAggregator _eventAggregator;
        public MainPageViewModel(INavigationService navigationService,
            Repositories.IItemRepository itemRepository,
            IEventAggregator eventAggregator)
        {
            _navigationService = navigationService;
            _itemRepository = itemRepository;
            _eventAggregator = eventAggregator;
        }

        public override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            foreach (var item in _itemRepository.AllItems())
                this.Items.Add(item);
            _eventAggregator.GetEvent<Message.NewValue>()
                .Subscribe(HandleValueChanged);
        }

        void Setup() {
            _eventAggregator.GetEvent<Message.NewValue>()
                   .Subscribe(HandleValueChanged);
        }

        private void HandleValueChanged(int value)
        {
            this.Value = value;
        }

        int _Value = default(int);
        public int Value { get { return _Value; } set { SetProperty(ref _Value, value); } }

        ObservableCollection<Models.Item> _Items = new ObservableCollection<Models.Item>();
        public ObservableCollection<Models.Item> Items { get { return _Items; } }

        Models.Item _Selected = default(Models.Item);
        public Models.Item Selected { get { return _Selected; } set { SetProperty(ref _Selected, value); } }

        DelegateCommand _AboutCommand = null;
        public DelegateCommand AboutCommand
        {
            get
            {
                if (_AboutCommand != null)
                    return _AboutCommand;
                _AboutCommand = new DelegateCommand
                (
                    () => { _navigationService.Navigate("About", null); },
                    () => { return true; }
                );
                this.PropertyChanged += (s, e) => _AboutCommand.RaiseCanExecuteChanged();
                return _AboutCommand;
            }
        }

        DelegateCommand _OpenCommand = null;
        public DelegateCommand OpenCommand
        {
            get
            {
                if (_OpenCommand != null)
                    return _OpenCommand;
                _OpenCommand = new DelegateCommand
                (
                    () => { _navigationService.Navigate("Detail", this.Selected.Text); },
                    () => { return this.Selected != null; }
                );
                this.PropertyChanged += (s, e) => _OpenCommand.RaiseCanExecuteChanged();
                return _OpenCommand;
            }
        }
    }
}
